{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 多神经元分类问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假如对单神经元的模型92.28%的准确率还不满意\n",
    "\n",
    "那么对于神经网络来说，可以添加多一些神经元\n",
    "\n",
    "那么，还是以MNIST手写数字识别问题为例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 数据读取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用网上的 MNIST 数据集(http://yann.lecun.com/exdb/mnist/)获取数据集压缩文件（切勿解压）：\n",
    "\n",
    "train-images-idx3-ubyte.gz --- 6万张28x28大小的训练数字图像\n",
    "\n",
    "train-labels-idx1-ubyte.gz --- 6万张训练图像的数字标记\n",
    "\n",
    "t10k-images-idx3-ubyte.gz  --- 1万张28x28大小的测试数字图像\n",
    "\n",
    "t10k-labels-idx1-ubyte.gz  --- 1万张测试图像的数字标记"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-f0ca348e51c4>:9: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ../data/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ../data/train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting ../data/t10k-images-idx3-ubyte.gz\n",
      "Extracting ../data/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "训练集数量： 55000\n",
      "验证集数量： 5000\n",
      "测试集数量： 10000\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "# 载入数据集\n",
    "mnist = input_data.read_data_sets(\"../data/\", one_hot=True)\n",
    "\n",
    "# 显示数据集数量\n",
    "print(\"训练集数量：\", mnist.train.num_examples)\n",
    "print(\"验证集数量：\", mnist.validation.num_examples)\n",
    "print(\"测试集数量：\", mnist.test.num_examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练图像大小： (55000, 784)\n",
      "训练标签大小： (55000, 10)\n"
     ]
    }
   ],
   "source": [
    "# 显示数据集大小\n",
    "print(\"训练图像大小：\", mnist.train.images.shape)\n",
    "print(\"训练标签大小：\", mnist.train.labels.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAADk5JREFUeJzt3W+IXfWdx/HPd237QNsH6iRpsHGnW8RMEDaNl7jgJmYpFrMUYkcqjVCzUDo+qH8CebA6JlREMSzbZH2wVCfb0KiJbSF/HxityOqksJRcRap1zFZkJs0mZO5gocYnRfPdB3NSpnHO71zvOfeem/m+XyBz7/meM+fLaT85987vnPMzdxeAeP6m7gYA1IPwA0ERfiAowg8ERfiBoAg/EBThB4Ii/EBQhB8I6nO93NnAwIAPDg72cpdAKJOTk5qZmbF21i0VfjO7TdKTki6T9F/uvj21/uDgoJrNZpldAkhoNBptr9vxx34zu0zSf0paL2mFpI1mtqLT3wegt8p8518t6T13f9/d/yzp55I2VNMWgG4rE/5rJP1hzvtT2bK/YmYjZtY0s2ar1SqxOwBVKhP++f6o8Kn7g919zN0b7t5YtGhRid0BqFKZ8J+StGzO+69IOl2uHQC9Uib8xyVdZ2ZfNbMvSPqupCPVtAWg2zoe6nP3j83sXkkvaXaob7e7/66yzgB0Valxfnd/QdILFfUCoIe4vBcIivADQRF+ICjCDwRF+IGgCD8QFOEHgiL8QFCEHw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figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "# 可视化图像\n",
    "def plot_image(image):\n",
    "    plt.imshow(image.reshape(28, 28), cmap='binary')\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# 可视化第二张训练图像\n",
    "plot_image(mnist.train.images[1])\n",
    "\n",
    "# 可视化第二张图像的标签\n",
    "print(mnist.train.labels[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 构建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    }
   ],
   "source": [
    "# 定义训练数据的占位符， x是784个像素点的特征值， y是10分类的标签值\n",
    "x = tf.placeholder(tf.float32, [None, 784], name=\"X\")\n",
    "y = tf.placeholder(tf.float32, [None, 10], name=\"Y\")\n",
    "\n",
    "H1_NN = 256  # 第一隐藏层神经元为256个\n",
    "H2_NN = 128  # 第二隐藏层神经元为128个\n",
    "\n",
    "# 第一隐藏层权重\n",
    "W1 = tf.Variable(tf.random_normal([784, H1_NN], stddev=0.01))\n",
    "b1 = tf.Variable(tf.zeros([H1_NN]))\n",
    "\n",
    "# 第二隐藏层权重\n",
    "W2 = tf.Variable(tf.random_normal([H1_NN, H2_NN], stddev=0.01))\n",
    "b2 = tf.Variable(tf.zeros([H2_NN]))\n",
    "\n",
    "# 输出层权重\n",
    "W3 = tf.Variable(tf.random_normal([H2_NN, 10], stddev=0.01))\n",
    "b3 = tf.Variable(tf.zeros([10]))\n",
    "\n",
    "# 定义向前传播计算\n",
    "A1 = tf.nn.relu(tf.matmul(x, W1) + b1)  # 使用relu激活函数计算第一隐藏层结果\n",
    "A2 = tf.nn.relu(tf.matmul(A1, W2) + b2)  # 使用relu激活函数计算第二隐藏层结果\n",
    "forward = tf.matmul(A2, W3) + b3  # 计算输出层结果\n",
    "\n",
    "# 定义分类结果softmax分类\n",
    "pred = tf.nn.softmax(forward)\n",
    "\n",
    "# 定义逻辑回归损失函数\n",
    "with tf.name_scope(\"LossFunction\"):\n",
    "    loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=forward, labels=y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train Epoch 01 Loss= 2.297379971 Accuracy= 0.1126\n",
      "Train Epoch 02 Loss= 2.201869488 Accuracy= 0.2440\n",
      "Train Epoch 03 Loss= 0.864152431 Accuracy= 0.7368\n",
      "Train Epoch 04 Loss= 0.612214446 Accuracy= 0.8122\n",
      "Train Epoch 05 Loss= 0.484201699 Accuracy= 0.8684\n",
      "Train Epoch 06 Loss= 0.405660808 Accuracy= 0.8916\n",
      "Train Epoch 07 Loss= 0.364046693 Accuracy= 0.9000\n",
      "Train Epoch 08 Loss= 0.335118026 Accuracy= 0.9074\n",
      "Train Epoch 09 Loss= 0.310631216 Accuracy= 0.9136\n",
      "Train Epoch 10 Loss= 0.290581882 Accuracy= 0.9206\n",
      "Train Epoch 11 Loss= 0.269258559 Accuracy= 0.9268\n",
      "Train Epoch 12 Loss= 0.252626538 Accuracy= 0.9318\n",
      "Train Epoch 13 Loss= 0.234416842 Accuracy= 0.9366\n",
      "Train Epoch 14 Loss= 0.219745994 Accuracy= 0.9414\n",
      "Train Epoch 15 Loss= 0.208215013 Accuracy= 0.9434\n",
      "Train Epoch 16 Loss= 0.193883806 Accuracy= 0.9464\n",
      "Train Epoch 17 Loss= 0.183618560 Accuracy= 0.9494\n",
      "Train Epoch 18 Loss= 0.175049528 Accuracy= 0.9516\n",
      "Train Epoch 19 Loss= 0.167848244 Accuracy= 0.9524\n",
      "Train Epoch 20 Loss= 0.157627955 Accuracy= 0.9580\n",
      "Train Epoch 21 Loss= 0.151573852 Accuracy= 0.9578\n",
      "Train Epoch 22 Loss= 0.145530015 Accuracy= 0.9604\n",
      "Train Epoch 23 Loss= 0.138701022 Accuracy= 0.9628\n",
      "Train Epoch 24 Loss= 0.134429425 Accuracy= 0.9626\n",
      "Train Epoch 25 Loss= 0.130623072 Accuracy= 0.9638\n",
      "Train Epoch 26 Loss= 0.126098812 Accuracy= 0.9648\n",
      "Train Epoch 27 Loss= 0.123115085 Accuracy= 0.9654\n",
      "Train Epoch 28 Loss= 0.119436391 Accuracy= 0.9660\n",
      "Train Epoch 29 Loss= 0.116216332 Accuracy= 0.9666\n",
      "Train Epoch 30 Loss= 0.113689661 Accuracy= 0.9678\n",
      "Train Epoch 31 Loss= 0.110156029 Accuracy= 0.9686\n",
      "Train Epoch 32 Loss= 0.106293105 Accuracy= 0.9698\n",
      "Train Epoch 33 Loss= 0.104758121 Accuracy= 0.9702\n",
      "Train Epoch 34 Loss= 0.102734402 Accuracy= 0.9720\n",
      "Train Epoch 35 Loss= 0.101247072 Accuracy= 0.9716\n",
      "Train Epoch 36 Loss= 0.097770184 Accuracy= 0.9724\n",
      "Train Epoch 37 Loss= 0.097097732 Accuracy= 0.9730\n",
      "Train Epoch 38 Loss= 0.094902001 Accuracy= 0.9738\n",
      "Train Epoch 39 Loss= 0.093059853 Accuracy= 0.9732\n",
      "Train Epoch 40 Loss= 0.092025951 Accuracy= 0.9742\n",
      "Train Epoch 41 Loss= 0.090748966 Accuracy= 0.9740\n",
      "Train Epoch 42 Loss= 0.089532666 Accuracy= 0.9750\n",
      "Train Epoch 43 Loss= 0.089387916 Accuracy= 0.9730\n",
      "Train Epoch 44 Loss= 0.087760024 Accuracy= 0.9736\n",
      "Train Epoch 45 Loss= 0.087011136 Accuracy= 0.9746\n",
      "Train Epoch 46 Loss= 0.085294686 Accuracy= 0.9752\n",
      "Train Epoch 47 Loss= 0.084949352 Accuracy= 0.9740\n",
      "Train Epoch 48 Loss= 0.081729874 Accuracy= 0.9760\n",
      "Train Epoch 49 Loss= 0.082520775 Accuracy= 0.9752\n",
      "Train Epoch 50 Loss= 0.080240369 Accuracy= 0.9768\n"
     ]
    }
   ],
   "source": [
    "train_epochs = 50  # 迭代次数\n",
    "learning_rate = 0.01  # 学习率\n",
    "\n",
    "# 梯度下降优化器 设置学习率和优化目标损失最小化\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)\n",
    "\n",
    "# 定义预测类别匹配情况\n",
    "correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
    "\n",
    "# 定义准确率，将布尔值转化成浮点数，再求平均值\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "sess = tf.Session()  # 建立会话\n",
    "init = tf.global_variables_initializer()  # 变量初始化\n",
    "sess.run(init)\n",
    "\n",
    "# 每个批次的大小，每次放入的大小，每次放入 50张图片 以矩阵的方式\n",
    "batch_size = 50\n",
    "\n",
    "# 计算一共有多少个批次，数量整除大小训练出有多少批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "# 批次迭代训练\n",
    "for epoch in range(train_epochs):\n",
    "    for batch in range(n_batch):\n",
    "        xs, ys = mnist.train.next_batch(batch_size)\n",
    "        sess.run(optimizer, feed_dict={x: xs, y: ys})\n",
    "    # 批次训练完成之后，使用验证数据计算误差与准确率\n",
    "    loss, acc = sess.run([loss_function, accuracy],\n",
    "                         feed_dict={x: mnist.validation.images,\n",
    "                                    y: mnist.validation.labels})\n",
    "    # 显示训练信息\n",
    "    print(\"Train Epoch\", '%02d' % (epoch + 1), \"Loss=\", '{:.9f}'.format(loss), \"Accuracy=\", \"{:.4f}\".format(acc))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 评估模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test Accuracy =  0.976\n",
      "Validation Accuracy =  0.9768\n",
      "Train Accuracy =  0.9884\n"
     ]
    }
   ],
   "source": [
    "# 测试集上评估模型预测的准确率\n",
    "accu_test = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})\n",
    "print(\"Test Accuracy = \", accu_test)\n",
    "\n",
    "# 验证集上评估模型预测的准确率\n",
    "accu_validation = sess.run(accuracy, feed_dict={x: mnist.validation.images, y: mnist.validation.labels})\n",
    "print(\"Validation Accuracy = \", accu_validation)\n",
    "\n",
    "# 训练集上评估模型预测的准确率\n",
    "accu_train = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels})\n",
    "print(\"Train Accuracy = \", accu_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 模型预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 6 1 1 1 3 9 5 2 9]\n"
     ]
    }
   ],
   "source": [
    "# 转换pred预测结果独热编码格式为数字0-9\n",
    "prediction_result = sess.run(tf.argmax(pred, 1), feed_dict={x: mnist.test.images})\n",
    "\n",
    "# 查看第200-209张测试图片的预测结果\n",
    "print(prediction_result[200:210])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAkQAAAEHCAYAAABPxFkFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB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ZVaoQw45JCo///zzDXq88O64rVu3btBjNTVz5szx8R133JF3uxEjRkTlddZZp8H6lDZ9+vSofP311/v4rrvuyrvfSivFfz/07NnTx+lbOHTu3LkePcye22+/PSoPHDgw77a9e/f2ca9evRqsTygOt8uoTunPtoULF/q4T58+UV3btm3L0qeGwhkiAACQeQyIAABA5jEgAgAAmddk1hAdcMABUTm8TXz49Gup8KfspvcrVF33y4J9993Xx//5z3+iunXXXdfHm266aYP248svv4zK48aN8/Hpp58e1S1YsKCgNpctWxaVw7Vq9957b1TX1C8/LYfwNgyjRo2K6pYsWeLj1VZbLapLb4vKUNvtEVA9WrRo4ePjjjuuEXtSepwhAgAAmceACAAAZF5FT5mFd5z+7LPPorrapsUKnTKr6z7Dhg3z8f777x/V7bfffkUfu5q8//77eevC2yO0a9eu5McOp8keeeSRqK5fv34+XmWVVaK6HXfcMW+bu+++u4/TT11H/Vx55ZU+fuKJJ6K68P146aWXlq1PqLt//etfjd0FlEj49If0Zffrr7++j/v27Vu2PpUDZ4gAAEDmMSACAACZV9FTZpVq8eLFPj7xxBOjuvAhtOHDaavVgw8+GJW/++67vNuW+m7UX331VVQ+44wzfHzPPfdEdeE02bXXXhvVnXXWWXmPEd6VlSmz0rriiit8nJ6y3nPPPX1cW34AlN6NN97o4/TD08PlB9WGM0QAACDzGBABAIDMY0AEAAAyr6LXELVv397Ha6+9dsnb32233Xw8c+bMqC685L826bscp28PUO1mzZoVlWu7i/eZZ55Z0mOPGTMmKofrhtKX1ofrhopZk/LAAw/krQvv2HrYYYcV3GaWnXbaaQVt99vf/tbHK6+8ckN1ByUU3lbjzjvvjOp++OGHcncHRVq6dKmPR48e7eM11lgj2u6ggw4qW5/KjTNEAAAg8xgQAQCAzCvLlNns2bN9HF6WLkm77rqrj7fddtuC2wynZtLTNJ06dfLxSSed5ONLLrkkb3tPPvlkVO7Vq1dBxy6mrhodfPDBUTm8lDp8eKckff755z6u6xTo1KlTfVzb1Ff4c7WibcNbBQwdOjSqu/XWW32cvjS8f//+Pu7SpUve9rNsypQpUfn+++/3cfhe+d3vfhdtt9deezVsx1ByPXr08HHLli2juvCBvEcccUTZ+oTCffDBBz5+9dVXfbzddttF24V37682nCECAACZx4AIAABkHgMiAACQeWVZQ/S///u/Pn7llVeiuvDS+meeeSaq22qrrXx89dVXR3XNmjXzcXrdzqBBg3y8zTbb5O1XuLbpwgsvjOrS60XySW9X6H7VIv39XWml/GPsAw880Mfjxo2L6jbccMOCjjdv3jwfp2+NEK5buOCCC6K6cJ3QW2+9FdUNHDjQxw8//HDeY4dPeZakm266qYAeZ9u7774blcPHrYTv/dNPP71sfULD6NChg4+z9jlYDUaMGFHj1/v06VPmnjQezhABAIDMY0AEAAAyryxTZrVNo4TTHr17947q7rvvPh+3a9cuqhsyZEhBxw5P2Y8cOTKqu/fee32cvlM16qZ169Y+Du98Kklvv/22j/fZZ5+oLryU89e//nVUt9lmm/k4fduGUHh56FprrRXVHXvssT5+6KGH8raRFk4DpG/NgBWr7U7fJ554oo832GCDcnQHQB7h78NQ27Zty9yTxsMZIgAAkHkMiAAAQOYxIAIAAJlXljVE4Vqg9Dqh8BLoOXPmRHU9e/YsqP30ZfeFXvIZ7lfXy0S7du0albfccss6tVMtnn76aR/vueeeUd0XX3zh43A9Ubp8yy23RHXh+p+w/bTwlg7p283XJsz98ccfH9Wdc845Pq7tFg74r8mTJ/v4iSeeyLvdAQccUI7uoBF07NixsbsAFI0zRAAAIPMYEAEAgMwry5RZ+PT59B2JZ8yYUe/26/qE+bru161bNx///e9/j+rStwfImu7du/t4/PjxUd0111zj48ceeyyqC+8knZbvctBitGjRwsfhndMl6ec//7mPwyfYo27CPC9ZsiSqW2211Xy82267la1PKK/wjvJAU8EZIgAAkHkMiAAAQOYxIAIAAJlXljVEoUsuuSQqjx07tuTHqMsl9MXsc+qpp/o462uGahOuJ5LiJ8lPmTIlqhs9enRBbYaX5H/00UdRXbhW7eSTT47qjjjiCB9vscUWBR0LhUmvE/r88899nH5fsUYrm3jsTeV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OOuooH99///1R3YQJE3wcPuJDkrbaaquCj5ElW2yxhY/TTy8Py+k1DLNnz/bxpEmTfJx+PMD+++/v4/TjHFBdnn76aR+nH690xBFHlLs7TU76UUfcQqbuOEMEAAAyjwERAADIvKqfMjv77LN9PHLkyKju4IMP9nF4yTZKI33n8U8++cTH6WmW0NixY6Py3nvv7eOvv/46736LFy/28dtvvx3VhbdwuOiii6K6/fbbz8dMkRXmiSee8HF6uvOjjz7y8Z577hnVvfvuuwW1f/nll/v4nHPOierCu2Rvt912Ud1KK/E3XlMTPln9+++/j+p22GGHcncHJTJx4sSoHH7OVio+PQAAQOYxIAIAAJnHgAgAAGReVawhSl+yG645eeGFF3y89dZbR9v94Q9/aNiOZVxtT0mubdvwyfdS/IiO8LL49K3gwznr9957L++x5s+fH5V/+tOf5t0WNQsvlU4LL61P57xjx44+Dt+n6ffmqFGjfDxw4MCoLiyHt2SQpPPOO6+WXqMSvfzyyz4++uijo7rNN9+83N1pEsJL65977rnG60gt1llnncbuQtE4QwQAADKPAREAAMi8qpgyC0+9S/E0WSh95+Sdd97Zxx06dIjq9thjDx+nn8weXsKN/MI7gUvStGnTCtpv/PjxBW03efLkqFzblBxKa+7cuQVtd8kll0Tlc88918errbZa3v0GDBjg4/TPw5FHHunj9C0Uwsvw058LqAwvvfRSVA6XPOy6667l7k6T9O233/p44cKFjdiT/C644IKoHN4KZejQoeXuTkE4QwQAADKPAREAAMi8JjtlFt6BOt8UmSRtv/32Pu7fv39UF15tFF7VIklXXHGFj8OHf0rSY4895uM11lijwB5nz1133RWVwzsMP//88+XuDkqoV69ePr777rujujDv4fSWJLVq1aqg9lu0aOHj9B1uH3roIR+HP1OSdPzxx/s4/b4NH0iLxpO+MnDLLbf08aGHHlru7jRJ4XKE8EHIkvTkk0+Wuzs1+uqrr6Jy+Hv6s88+i+rWXnvtsvRpRThDBAAAMo8BEQAAyDwGRAAAIPOa7BqiM844w8err756VBc+xX6DDTbwcfv27fO2d+mll0bl2267zcenn356VBfOhfbu3bvAHmfPWmutFZXHjBnj4wULFkR1t9xyS952jjvuOB+Ht0cIn7guSb/85S/r1E8U7/rrr/fxLrvsEtX169evQY+97777+jhcMyRJt956q4/Tl/2GdzwvdC0TpNdff93H6Uu8P/7444LaGDdunI/T6zXPOussH7MmszArr7yyj9NPXAjv5j9nzpyy9WlFwtuupG/bwRoiAACACsGACAAAZF6TnTLbbLPNfHz55ZfXu71mzZpF5dNOO83HV199dVQXTtftvvvuUV2bNm3q3ZdqFd7he+ONN47q0pfiFuLVV1+Nys65vNvusMMONfYDdbPmmmv6+KSTTmq0fhx++OFROZxGfeSRR6K6d99918fdunVr2I41MR988IGPjz322Khu5syZPv7Pf/4T1X399df1PnY4lZmekgvzm/6sTU/JZ9X//M//ROVwyuyHH36od/vpO4uH752bbropqnv00Ud9HP7cpA0ZMiQqDxs2rD5dLBnOEAEAgMxjQAQAADKPAREAAMi8JruGqDGF8+3pOXXWEJVP+lL92p5237VrVx+zhqh6bL755lG5Z8+ePh4+fHhUF5azvoYofPyJFN+i4P333y9rXxYtWuTjBx54IKoLy1tttVVU16NHDx/feOONUd0XX3zh48WLF0d1H374oY/32muvOvS4sq2yyiolbW/vvffOW/f73/8+b/mAAw6I6p566ikfT5o0KaoLH+XRmJfgc4YIAABkHgMiAACQeUyZIRMOO+ywxu5CZsyfP9/H6dPf4R12S2GjjTaKyuFTwJHfG2+8EZVLPU2WzsO6667r42XLlkV1K61U2N/lM2bMiMofffSRj9OXeIdPWk8/df2EE07wcTVOmVWqcElDuOxEkr788ksfM2UGAADQiBgQAQCAzGNABAAAMo81RHlMnz7dx+nLNrfffnsft27dumx9Qt117ty5sbtQtWbNmhWVd9ttNx937Ngxqrvhhht8HF4iXyrvvPNOydusFiNGjPBx+tEJobPPPjvvfumnlIe23HJLH48ePTqq22STTQruZz4TJkyIyp9//nlB+zVvHv+a6927d737gvo58MADo3KlfD5zhggAAGQeAyIAAJB5TJkl3nzzzai8zz77+HjJkiVR3e233+5jpszK689//nNB24X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ewnWdTz31VCP2pG44QwQAADKPAREAAMg8c84VvrHZp5LmrHBDNJROzrmS3eKTfDa6kuWTXDY63pvVhfdm9Sg4l0UNiAAAAKoRU2YAACDzGBABAIDMY0AEAAAyjwERAADIPAZEAAAg8xgQAQCAzGNABAAAMo8BEQAAyDwGRAAAIPP+H/NdoOfVZ7G7AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 720x864 with 10 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 定义比对可视化函数\n",
    "def plot_images_labels_prediction(images,  # 图像列表\n",
    "                                  labels,  # 标签列表\n",
    "                                  prediction,  # 预测值列表\n",
    "                                  index,  # 开始显示的索引\n",
    "                                  num=5):  # 缺省一次显示5张\n",
    "    fig = plt.gcf()  # 获取当前图表，get current figure\n",
    "    fig.set_size_inches(10, 12)  # 1英寸等于2.54cm\n",
    "\n",
    "    if num > 25:  # 最多显示25张图片\n",
    "        num = 25\n",
    "\n",
    "    for i in range(0, num):\n",
    "        ax = plt.subplot(5, 5, i + 1)  # 获取当前要处理的图片\n",
    "        ax.imshow(np.reshape(images[index], (28, 28)), cmap='binary')  # 显示第index个图像\n",
    "        title = 'label = ' + str(np.argmax(labels[index]))  # 显示标签的标题\n",
    "        if len(prediction) > 0:  # 如果有预测结果的话，添加显示预测的标题\n",
    "            title += ',predict = ' + str(prediction[index])\n",
    "        ax.set_title(title, fontsize=10)  # 显示图上的标题\n",
    "        # 不显示坐标轴\n",
    "        ax.set_xticks([])\n",
    "        ax.set_yticks([])\n",
    "        index += 1\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# 可视化第200-209张测试图片的预测结果对比\n",
    "plot_images_labels_prediction(mnist.test.images,\n",
    "                              mnist.test.labels,\n",
    "                              prediction_result,\n",
    "                              200, 10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
